import framebuf
from machine import Pin,SPI
import utime
class screen(framebuf.FrameBuffer):
    def __init__(self,w,h,x,y):
        self.width=w
        self.hight=h//8
        self.x =x
        self.y = y//8
        self.buf=bytearray(w*h//8)
        self.lcdRS=Pin(4,Pin.OPEN_DRAIN,1)

        self.hspi = SPI(1, 20000000, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
        super().__init__(self.buf,self.width,self.hight*8,framebuf.MONO_VLSB)
        self.tls=[]

    def print(self,s):
        self.lcdAddress(self.x,self.y,self.width,self.hight*8)
        self.fill(0)
        self.lcdData(self.buf)
        while len(s)>0 :
            self.tls.append(s[:self.width//8])
            if len(self.tls)>(self.hight):
                self.tls.pop(0)
            s=s[self.width//8:]
        for i,v in enumerate(self.tls):
            self.text(v,0,i*8)
        self.lcdAddress(self.x,self.y,self.width,self.hight*8)
        self.lcdData(self.buf)

    def te(self):
        self.fill(1)
        self.lcdAddress(self.x,self.y,self.width,self.hight*8)
        self.lcdData(self.buf)


    def lcdData(self,da):
        self.lcdRS.value(1)
        #utime.sleep_us(1)
        self.hspi.write(da)

    def lcdCmd(self,cmd):
        self.lcdRS.value(0)
        self.hspi.write(cmd)

    def lcdinit(self):
        lcdRST=Pin(2,Pin.OUT)
        lcdRST.value(0)
        utime.sleep_ms(1)
        lcdRST.value(1)
        self.lcdCmd(b'\x30\x94') #退出睡眠模式
        #lcdCmd(b'\x31\x7b') #升压倍数 ？\x7b
        #lcdData(b"\x9f")
        self.lcdCmd(b"\x32") #偏压比设置
        self.lcdData(b"\x00\x01\x02")

        #lcdCmd(b"\x31\xf2")#温度范围设置
        #lcdData(b"\x1e\x28\x32")
        '''
        lcdCmd(b"\x20") #灰度级设置
        lcdData(b"\x01\x03\x05\x07\x09\x0b\x0d\x10\x11\x13\x15\x17\x19\x1b\x1d\x1f")
        '''
        self.lcdCmd(b"\x30\x75") #页地址设置 #？\x30\x75 
        self.lcdData(b"\x00\x14")
        self.lcdCmd(b"\x15") #列地址设置
        self.lcdData(b"\x00\xff")

        self.lcdCmd(b"\xbc") #行列扫描方向
        self.lcdData(b"\x00") # \x00\xa6 ?\xa6

        self.lcdCmd(b"\xca") #显示控制
        self.lcdData(b"\x00\x7f\x20") #设置CL驱动频率：CLD=0 ， 点空比：Duty=128，帧周期

        self.lcdCmd(b"\xf0") #显示模式设置
        self.lcdData(b"\x10") #黑白模式

        self.lcdCmd(b"\x81") #设置对比度
        self.lcdData(b"\x39\x04")

        self.lcdCmd(b"\x20") #电源控制
        self.lcdData(b"\x0b") # VB VF VR =1

        utime.sleep_us(100)
        self.lcdCmd(b"\xaf") #显示
        self.lcdCmd(b"\x0c") #数据格式选择 D0->D7
        self.lcdClean()


    def lcdAddress(self,x,y,x_total,y_total):
        self.lcdCmd(b"\x15")
        self.lcdData(x.to_bytes(1,"big"))
        self.lcdData((x+x_total-1).to_bytes(1,"big"))
        self.lcdCmd(b"\x30\x75")
        self.lcdData(y.to_bytes(1,"big"))
        self.lcdData((y+y_total-1).to_bytes(1,"big"))
        self.lcdCmd(b"\x30\x5c")
    def lcdClean(self):
        buf=bytes()
        self.lcdAddress(0,0,256,16)
        for i in range(0,256*16):
            buf=buf+b"\x00"
        self.lcdData(buf)
